/**
 * 参数设置--通讯接口
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>通讯接口</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="">
        <el-input   
          size="small"
          v-model="getList.keyword"
          placeholder="输入通信接口名称过滤"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-refresh"
          @click="refresh()"
          >刷新</el-button
        >
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="add()"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table
      size="small"
      :data="listData"
      highlight-current-row
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="Name"
        label="接口名称"
        width="180"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="Type"
        label="接口类型"
        width="180"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button
            @click="details(scope.row)"
            type="info"
            size="mini"
            icon="el-icon-document"
            >详细参数</el-button
          >
          <el-button size="mini" type="success" @click="update(scope.row)"
            >编辑</el-button
          >
          <el-button size="mini" @click="deleteUser(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="添加通讯接口" :visible.sync="addShow" width="40%">
      <el-descriptions :column="1">
        <el-descriptions-item label="接口名称">
          <el-input
            style="margin-top: -10px"
            v-model="getList.Name"
            placeholder="接口名称新增后不可修改"
          ></el-input>
        </el-descriptions-item>
        <el-descriptions-item label="接口类型"
          ><el-select v-model="getList.Type" placeholder="请选择">
            <el-option
              v-for="item in options2"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-descriptions-item>
        <el-descriptions-item label="目标端口"
          ><el-input v-model="Port"></el-input
        ></el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addContent">确定</el-button>
        <el-button @click="addShow = false">取消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改通讯接口" :visible.sync="updateShow" width="40%">
      <el-descriptions :column="1">
        <el-descriptions-item label="接口名称">
          <el-input
            style="margin-top: -10px"
            v-model="getList.Name"
            placeholder="接口名称不可修改"
            disabled
          ></el-input>
        </el-descriptions-item>
        <el-descriptions-item label="接口类型"
          ><el-select
            v-model="getList.Type"
            placeholder="请选择"
            :disabled="updateDisabled"
          >
            <el-option
              v-for="item in options2"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-descriptions-item>
        <el-descriptions-item label="目标端口"
          ><el-input v-model="Port"></el-input
        ></el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateContent">确定</el-button>
        <el-button @click="updateShow = false">取消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="详细参数" :visible.sync="show" width="40%">
      <el-descriptions :column="1">
        <el-descriptions-item
          label="物理接口"
          v-if="InterfaceMaptype === 'LocalSerial'"
          >{{ systemLogform.Name }}</el-descriptions-item
        >
        <el-descriptions-item
          label="波特率"
          v-if="InterfaceMaptype === 'LocalSerial'"
          >{{ systemLogform.BaudRate }}
        </el-descriptions-item>
        <el-descriptions-item
          label="数据位"
          v-if="InterfaceMaptype === 'LocalSerial'"
          >{{ systemLogform.DataBits }}</el-descriptions-item
        >
        <el-descriptions-item
          label="校验位"
          v-if="InterfaceMaptype === 'LocalSerial'"
          >{{ systemLogform.Parity }}</el-descriptions-item
        >
        <el-descriptions-item
          label="停止位"
          v-if="InterfaceMaptype === 'LocalSerial'"
          >{{ systemLogform.StopBits }}</el-descriptions-item
        >
        <el-descriptions-item
          label="目标IP"
          v-if="InterfaceMaptype === 'TcpClient'"
          >{{ systemLogform.IP }}</el-descriptions-item
        >
        <el-descriptions-item
          label="目标端口"
          v-if="InterfaceMaptype === 'TcpClient'"
          >{{ systemLogform.Port }}</el-descriptions-item
        >
        <!-- <el-descriptions-item label="命令间隔">{{
          systemLogform.Timeout
        }}</el-descriptions-item>
         <el-descriptions-item label="超时时间">{{
          systemLogform.Interval
        }}</el-descriptions-item>
       <el-descriptions-item label="监听端口">{{
          systemLogform.Port
        }}</el-descriptions-item>
        <el-descriptions-item label="通讯超时">{{
          systemLogform.Timeout
        }}</el-descriptions-item>
        <el-descriptions-item label="通讯间隔">{{
          systemLogform.Interval
        }}</el-descriptions-item> -->
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="show = false">关闭</el-button>
      </div>
    </el-dialog>
    <div class="tabListPage">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="formInline.page"
        :page-sizes="pageSizes"
        :page-size="formInline.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { isReactive } from "vue";
// import {addPoints1} from '../../api/teshushenhe'
import { deptList, deptSave, deptUpdate, deptDelete } from "../../api/userMG";
// import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
      updateDisabled: false,
      updateShow: false,
      addShow: false,
      show: false, //控制弹窗显示
      detailsList: {},
      systemLogform: {
        Name: "",
        BaudRate: "",
        DataBits: "",
        StopBits: "",
        Parity: "",
        Timeout: "",
        Interval: "",
        IP: "",
        Port: "",
      },
      InterfaceMaptype: "",
      listData: [],
      getList: {
        Name: "",
        Type: "",
        Param: {
          Port: "",
        },
      },
      Port: "",
      options2: [
        {
          value: 1,
          label: "serial1",
        },
        {
          value: 2,
          label: "tcpc1",
        },
        {
          value: 3,
          label: "tcpc2",
        },
        {
          value: 4,
          label: "tcps1",
        },
        {
          value: 5,
          label: "tcps2",
        },
        {
          value: 6,
          label: "输入1",
        },
        {
          value: 7,
          label: "输出1",
        },
      ],
      isloading: false,
      // 默认显示第几页
      currentPage: 1,
      // 总条数，根据接口获取数据长度(注意：这里不能为空)
      totalCount: 0,
      // 个数选择器（可修改）
      pageSizes: [10, 20, 30, 40],
      // 默认每页显示的条数（可修改）
      PageSize: 10,
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏

      formInline: {
        page: 1,
        limit: 10,
        varLable: "",
        varName: "",
        token: localStorage.getItem("logintoken"),
      },

      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10,
      },
    };
  },
  // 注册组件
  components: {
    // Pagination
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  // created() {

  // },
  mounted() {
    this.getDeptList();
  },
  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 分页
    // 每页显示的条数
    handleSizeChange(val) {
      // 改变每页显示的条数
      this.formInline.page = 1;
      this.formInline.limit = val;
      // 点击每页显示的条数时，显示第一页
      this.getDeptList();
      // 注意：在改变每页显示的条数时，要将页码显示到第一页
      // this.currentPage = 1;
    },
    // 显示第几页
    handleCurrentChange(val) {
      // 改变默认的页数
      this.formInline.page = val;
      // 切换页码时，要获取每页显示的条数
      this.getDeptList();
    },
    // 通讯接口--渲染数据
    getDeptList() {
      
      deptList({
        // 显示第几页
        current: this.formInline.page,
        // 每页显示的条数
        sizePage: this.formInline.limit,
      }).then((res) => {
        console.log(res);
        this.listData = res.Data.InterfaceMap;
        /* 分页赋值
        this.formInline.page = res.data.index;
        this.formInline.limit = res.data.pageSize;
        this.totalCount = res.data.totalCount;*/
      });
    },
    // 详细参数--渲染数据
    details(row) {
      this.show = true;
      this.InterfaceMaptype = row.Type;
      this.systemLogform.Name = row.Param.Name;
      this.systemLogform.BaudRate = row.Param.BaudRate;
      this.systemLogform.DataBits = row.Param.DataBits;
      this.systemLogform.StopBits = row.Param.StopBits;
      this.systemLogform.Parity = row.Param.Parity;
      this.systemLogform.Timeout = row.Param.Timeout;
      this.systemLogform.IP = row.Param.IP;
      this.systemLogform.Port = row.Param.Port;
    },
    add() {
      this.addShow = true;
    },
    // 添加通讯接口
    addContent() {
      this.getList.Param.Port = this.Port;
      // this.getList.Param=JSON.stringify(this.getList.Param)
      // console.log(this.getList.Param);
      // console.log(JSON.stringify(this.getList));
      // this.getList.Param=JSON.parse(this.getList.Param)
      deptSave(this.getList)
        .then((res) => {
          console.log(res);
          this.$message({
            message: res.Message,
            type: "success",
          });
        })
        .catch((error) => {
          this.$message.error(error.Message);
        });
    },
    // 刷新
    refresh() {
      this.loading = true;
      setTimeout(async () => {
        await this.getDeptList(); // 这里是拿到数据
        this.loading = false; // 计时器结束 停止转圈圈
      }, 1000);
    },
    update(row) {
      this.updateShow = true;
      this.getList.Name = row.Name;
      this.getList.Type = row.Type;
      this.Port = row.Param.Port;
    },
    // 编辑通讯接口
    updateContent() {
      this.getList.Param.Port = this.Port;
      deptUpdate(this.getList)
        .then((res) => {
          console.log(res);
          this.$message({
            message: res.Message,
            type: "success",
          });
        })
        .catch((error) => {
          this.$message.error(error.Message);
        });
    },
    //删除通讯接口
    deleteUser(row) {
      console.log(row.Name);
      this.$confirm("此操作将永久删除该条信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deptDelete({ Name: row.Name }).then((res) => {
            console.log(res);
            if(res.Code===0){
              // 删除成功提示
                this.$message({showClose: true, message: res.Message,type: 'success', duration:1000,center:true});
                // 重新刷新最新的用户列表
                this.getDeptList();
              }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-left:-60%;
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
.input2 {
  width: 380px;
  margin-left: 12px;
}
</style>

 